home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0024_Faster File Exists.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  2KB  |  75 lines

  1. {
  2. SEAN PALMER
  3.  
  4. I just ran some timings, which are gonna be affected by SMARTDRV.EXE
  5. being loaded, but I took that into account (ran multiple times on same
  6. file, and took timings on second/subsequent runs, to make sure always
  7. got cache hits)
  8.  
  9. What I got was that FileExists below and my modified version of that
  10. fileExist3 function that's been floating around this echo for a while
  11. (no bug) both run neck and neck... it's amazing... both are slightly
  12. faster than FileExist2 and lots lots faster than the 'reset,
  13. fileExist=(ioresult=0)' type thing that most people still seem to use...
  14.  
  15. I'd recommend using the first one below as it's really short...
  16. }
  17.  
  18. uses
  19.   dos;
  20.  
  21. { Tied for fastest }
  22. function fileExists(var s : string) : boolean;
  23. begin
  24.   fileExists := fSearch(s, '') <> '';
  25. end;
  26.  
  27. { 2nd }
  28. function fileExist2(var s : string) : boolean;
  29. var
  30.   r : searchrec;
  31. begin
  32.   findfirst(s, anyfile, r);
  33.   fileExist2 := (dosError = 0);
  34. end;
  35.  
  36. { Tied for fastest }
  37. function fileExist3(var s : string) : boolean; assembler;
  38. asm
  39.   push ds
  40.   lds  si, s        { need to make ASCIIZ }
  41.   cld
  42.   lodsb             { get length; si now points to first char }
  43.   xor  ah, ah
  44.   mov  bx, ax
  45.   mov  al, [si+bx]  { save byte before placing terminating null }
  46.   push ax
  47.   mov  byte ptr [si+bx],0
  48.   mov  dx, si
  49.   mov  ax, $4300    { get file attributes }
  50.   int  $21
  51.   mov  al, 1        { if carry set, fail }
  52.   pop  dx
  53.   mov  [si+bx], dl  { restore byte }
  54.   pop  ds
  55. end;
  56.  
  57. { Slowest }
  58. function fileExist4(var s : string) : boolean;
  59. var
  60.   f : file;
  61. begin
  62.   assign(f,s);
  63.   {$I-}
  64.   reset(f);
  65.   {$I+}
  66.   if ioresult = 0 then
  67.   begin
  68.     close(f);
  69.     fileExist4 := true;
  70.   end
  71.   else
  72.     fileExist4 := false;
  73. end;
  74.  
  75.